package simplecoder.wizards;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import simplecoder.domain.Field;
import simplecoder.domain.Module;

public class CodeGenerationEdit extends ApplicationWindow {
	public CodeGenerationEdit(Shell shell) {
		super(shell);
	}

	protected Control createContents(Composite parent) {
		getShell().setText("选择要生成到编辑表单的字段");
		parent.setSize(400, 400);
		
		center(getShell().getDisplay(), getShell());
		
		//创建一个新的Composite
		Composite container = new Composite(parent, SWT.NULL);
		//设置网格布局
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		container.setLayout(layout);
		
		
		final List<Field> fields = Module.getInstance().getPojo().getFieldList();
		// 定义按钮为数组，要在事件处理类中使用按钮的引用，要设置成final型
		final List<Button> buttonList = new ArrayList<Button>();
		final List<Combo> comboList = new ArrayList<Combo>();
		
		for (Field field : fields) {
			Button button = new Button(container, SWT.CHECK);
			button.setText(field.getName());
			if(field.getIsEdit()){
				button.setSelection(true);
			}
			buttonList.add(button);
			
			Combo combo = new Combo(container, SWT.ARROW);
			String[] items = new String[4];
			items[0]="text";
			items[1]="textarea";
			items[2]="datetime";
			items[3]="state";
			combo.setItems(items);
			combo.setText(field.getInput());
			
			comboList.add(combo);
		}
		
		// 确定按钮
		Button ok = new Button(container, SWT.PUSH);
		ok.setText("确定");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				for (Button button : buttonList) {
					// 如果选中，则输出选中的值
					if (button.getSelection()){
						System.out.println("您选择了：" + button.getText());
						Module.getInstance().getPojo().addEditField(button.getText());
					}
				}
				for (int i = 0; i < comboList.size(); i++) {
					Combo combo = comboList.get(i);
					fields.get(i).setInput(combo.getText());
				}
				getShell().setVisible(false);
			}
		});
		
		
		
		return parent;
	}
	
	  /**
     * 设置窗口位于屏幕中间
     * @param display 设备
     * @param shell 要调整位置的窗口对象
     */
    public static void center(Display display, Shell shell)
    {
        Rectangle bounds = display.getPrimaryMonitor().getBounds();
        Rectangle rect = shell.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        shell.setLocation(x, y);
    } 
}
